package com.ly.hotel.spainterface.jyt.utils;

import android.content.Context;
import android.widget.Toast;

import com.ly.hotel.spainterface.jyt.bean.PluginBean;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import java.io.ByteArrayInputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

/**
 * Created by ThinkPad on 2018/3/20.
 */

public class JytXmlParseUtils {

    private static JytXmlParseUtils xmlParseUtils;
    private DocumentBuilder builder = null;

    public static JytXmlParseUtils getInstance(){
        if(xmlParseUtils == null){
            xmlParseUtils = new JytXmlParseUtils();
        }
        return xmlParseUtils;
    }

    private JytXmlParseUtils(){
        DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
        try {
            builder = domfac.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }
    }

    // 解析插件配置
    public PluginBean parseJishiPluginConfig(Context context, String pluginConfig, String pluginVersion){
        PluginBean pluginBean = null;
        try {
            Document doc = builder.parse(new ByteArrayInputStream(pluginConfig.getBytes("utf-8")));
            NodeList pluginVersionNode = doc.getElementsByTagName(pluginVersion);// 获得插件版本号下的节点
            pluginBean = new PluginBean();
            for(int i = 0;i < pluginVersionNode.getLength();i ++){
                NodeList jishiNode = ((Element) pluginVersionNode.item(i)).getElementsByTagName("jishi");// 获得xiaofei下的节点
                if(jishiNode.getLength() >= 1){
                    // 解析ip节点
                    NodeList headNode = ((Element) jishiNode.item(0)).getElementsByTagName("ip");
                    if (headNode.getLength() >= 1 && headNode.item(0) != null) {
                        try {
                            String ip = headNode.item(0).getFirstChild().getNodeValue().trim();
                            pluginBean.setIp(ip);
                        } catch (Exception e) {
                            Toast.makeText(context, "ip配置有误！", Toast.LENGTH_SHORT).show();
                            pluginBean.setPort("");
                        }
                    }
                    // 解析port节点
                    headNode = ((Element) jishiNode.item(0)).getElementsByTagName("port");
                    if (headNode.getLength() >= 1 && headNode.item(0) != null) {
                        try {
                            String port = headNode.item(0).getFirstChild().getNodeValue().trim();
                            pluginBean.setPort(port);
                        } catch (Exception e) {
                            Toast.makeText(context, "port配置有误！", Toast.LENGTH_SHORT).show();
                            pluginBean.setPort("");
                        }
                    }


                }
            }
            return pluginBean;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pluginBean;
    }
}
